package com.clark.crawler.novel.property;

import org.apache.commons.collections.map.HashedMap;
import org.ho.yaml.Yaml;

import java.io.FileNotFoundException;
import java.util.List;
import java.util.Map;

/**
 * description:
 *
 * @author tianhua.xie（tianhua.xie@ucarinc.com）
 * @version 1.0
 * @date 2018/12/27 9:50
 */
public class NovelPropertyUtil {
    private static Map<String,Object> configMap;
    static {
        try {
            configMap = Yaml.loadType(NovelPropertyUtil.class.getClassLoader().getResourceAsStream("application-novel.yml"), HashedMap.class);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException("application-novel.yml 初始化失败");
        }
    }
    /**
     * description:
     *
     * @author tianhua.xie（tianhua.xie@ucarinc.com）
     * @date 2018-12-27 09:56:23
     * @param
     * @return java.util.Map<java.lang.String,java.lang.Object>
     **/
    public static Map<String,Object> getProperty(){
        if (null == configMap){
            return null;
        }

        Map<String,Object> crawlerMap;
        Map<String,Object> novelCfgMap = null;
        List<Map<String,Object>> novelCfgList;

        try {
            crawlerMap = (Map<String, Object>) configMap.get("crawler");
            novelCfgList = (List<Map<String, Object>>) configMap.get("novelCfg");

            Boolean isDev = (Boolean) crawlerMap.get("dev");
            Integer devCrawlerCnt = (Integer) crawlerMap.get("devCrawlerCnt");
            String chapterDealFactoryName = (String) crawlerMap.get("chapterDealFactoryName");
            String novelUrl = (String) crawlerMap.get("novelUrl");

            for (Map<String, Object> map: novelCfgList){
                String name = (String) map.get("name");
                if (chapterDealFactoryName.equals(name)){
                    novelCfgMap = map;
                    novelCfgMap.put("dev",isDev);
                    novelCfgMap.put("devCrawlerCnt",devCrawlerCnt);
                    novelCfgMap.put("novelUrl",novelUrl);
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("配置初始化失败");
        }
        if (null == novelCfgMap){
            throw new RuntimeException("没有获取的配置");
        }

        return novelCfgMap;
    }

    public static String getChapterDealFactoryClassName(){
        Map<String,Object> crawlerMap = (Map<String, Object>) configMap.get("crawler");
        String chapterDealFactoryName = (String) crawlerMap.get("chapterDealFactoryName");
        if (null == chapterDealFactoryName){
            throw new RuntimeException("没有获取的配置");
        }

        return String.format("com.clark.crawler.novel.factory.%sChapterDealFactory",chapterDealFactoryName);
    }

    public static String getFilePath(){
        Map<String,Object> crawlerMap = (Map<String, Object>) configMap.get("crawler");

        String filePath = (String) crawlerMap.get("filePath");
        String novelName = (String) crawlerMap.get("novelName");

        return String.format("%s/%s.txt",filePath,novelName);
    }




}
